<?php
namespace app\index\controller;

use app\common\component\fruit\FruitService;

class Lottery extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}


	public function index() {
		return $this -> view -> fetch();
	}

	public function lottery() {
		$userId = \think\Session::get('user_id');
		$fruitServiceFind = (new FruitService()) -> find($userId);

		$this -> view -> assign('fruit', $fruitServiceFind['data']['fruit']);

		$config = include(APP_PATH . 'config/lotteryConfig.php');
		$this -> view -> assign('config', $config);

		return $this -> view -> fetch();
	}

	public function handleLottery() {
		$post = $this -> request -> post();
		switch($post['need_guozi']) {
			case 1:
				$fruitPrice = 'fruit_first';
				$fruitLog = '\\app\\common\\component\\fruit\\FruitFirstLogService';
				break;
			case 2:
				$fruitPrice = 'fruit_second_profit';
				$fruitLog = '\\app\\common\\component\\fruit\\FruitSecondLogService';
				break;
			default:
				return ['code' => -1, 'message' => '请选择果子'];
		}

		$userId = \think\Session::get('user_id');
		$fruitServiceFind = (new FruitService()) -> find($userId);

		$config = include(APP_PATH . 'config/lotteryConfig.php');
		if($config['fruit'][$fruitPrice]['value'] > $fruitServiceFind['data']['fruit'][$fruitPrice]) {
			return ['code' => -1, 'message' => '当前选择的果子少于' . $config['fruit'][$fruitPrice]['value'] . ', 暂时无法抽奖'];
		}

		$fruitServiceFind['data']['fruit'] -> $fruitPrice -= $config['fruit'][$fruitPrice]['value'];
		\think\Db::startTrans();
		try {
			if(!$fruitServiceFind['data']['fruit'] -> save()) {
				throw new \Exception('抽奖失败, fruit save!');
			}

			$fruitLogServiceCreate = (new $fruitLog()) -> create([
				'user_id' => $userId,
				'type' => 6,
				'active' => 0,
				'price' => $config['fruit'][$fruitPrice]['value'],
				'create_time' => time(),
				'remark' => '抽奖大转盘'
			]);
			if($fruitLogServiceCreate['code'] != 0) {
				throw new \Exception($fruitLogServiceCreate['message']);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return ['code' => -1, 'message' => $e -> getMessage()];
		}

		return ['code' => 0, 'message' => '成功'];
	}

	public function getRand() {
		$value = crypto_rand(1, 10);

		$config = include(APP_PATH . 'config/lotteryConfig.php');
		if($config['restaraunts'][$value - 1]['reward'] != 0) {
			$post = $this -> request -> post();
			switch($post['need_guozi']) {
				case 1:
					$fruitPrice = 'fruit_first';
					$fruitLog = '\\app\\common\\component\\fruit\\FruitFirstLogService';
					break;
				case 2:
					$fruitPrice = 'fruit_second_profit';
					$fruitLog = '\\app\\common\\component\\fruit\\FruitSecondLogService';
					break;
				default:
					return ['code' => -1, 'message' => '请选择果子'];
			}

			$userId = \think\Session::get('user_id');
			\think\Db::startTrans();
			try {
				if(!\app\common\model\Fruit::where('user_id', $userId) -> setInc($fruitPrice, $config['restaraunts'][$value - 1]['reward'])) {
					throw new \Exception('抽奖失败, fruit setInc!');
				}

				$fruitLogServiceCreate = (new $fruitLog()) -> create([
					'user_id' => $userId,
					'type' => 6,
					'active' => 1,
					'price' => $config['restaraunts'][$value - 1]['reward'],
					'create_time' => time(),
					'remark' => '抽奖奖励' . $config['restaraunts'][$value - 1]['name'] . ', 获取' . $config['restaraunts'][$value - 1]['reward'] . '果子'
				]);
				if($fruitLogServiceCreate['code'] != 0) {
					throw new \Exception($fruitLogServiceCreate['message']);
				}

				\think\Db::commit();
			} catch(\Exception $e) {
				\think\Db::rollback();
	
				return ['code' => -1, 'message' => $e -> getMessage()];
			}
		}

		return $value;
	}
}
?>